package com.qing.spark.dao

import java.util

import com.qing.spark.beans.Message

import scala.collection.mutable._
import scala.collection.mutable.ListBuffer

/**
  * Created by Administrator on 2018/3/16 0016.
  */
case class DayCount(day: Int, packageSize: Int, dataSize: Int)

object DayCountDao {
  val tableName = "day_count"

  val family = "data"

  val qualifier1 = "packageSize"

  val qualifier2 = "dataSize"

  def save(lists: ListBuffer[DayCount]): Unit = {
//    val table = HBaseUtils.getInstance.getTable(tableName)
//    for (temp <- lists) {
//      table.incrementColumnValue(String.valueOf(temp.day).getBytes,
//        family.getBytes, qualifier1.getBytes, temp.packageSize)
//
//      table.incrementColumnValue(String.valueOf(temp.day).getBytes,
//        family.getBytes, qualifier2.getBytes, temp.dataSize)
//    }

    val con = MysqlUtils.getInstance.getCon()

    for (temp <- lists) {
      //          table.incrementColumnValue(Bytes.toBytes(temp.hour),
      //            Bytes.toBytes(family), Bytes.toBytes(qualifier1), temp.packageSize)
      //
      //          table.incrementColumnValue(Bytes.toBytes(temp.hour),
      //            Bytes.toBytes(family), Bytes.toBytes(qualifier2), temp.dataSize)

      val url = "select * from day_count where day=" + temp.day
      MysqlUtils.getInstance.reCon()
      val result = con.prepareStatement(url).executeQuery()
      if (result.next()) {
        //已经存在
        val newDataSize = result.getLong("dataSize") + temp.dataSize
        val newPackageSize = result.getLong("packageSize") + temp.packageSize
        val psql = con.prepareStatement("update day_count set dataSize = ?,packageSize = ? where day = ?");
        psql.setLong(1, newDataSize)
        psql.setLong(2, newPackageSize)
        psql.setLong(3, temp.day)
        psql.executeUpdate()
      } else {
        //不存在
        val sql = "insert into day_count(day,dataSize,packageSize) values(?,?,?)"
        val ptmt = con.prepareStatement(sql)
        ptmt.setLong(1, temp.day)
        ptmt.setLong(2, temp.dataSize)
        ptmt.setLong(3, temp.packageSize)
        ptmt.execute()
      }


    }



  }
}
